
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>

//#include "sbpcap.h"
#include "os/osapi.h"

#ifndef ERROR
#define ERROR -1
#endif

#ifndef OK
#define OK 0
#endif

#ifndef TRUE
#define TRUE 1
#endif

#ifndef FALSE
#define FALSE 0
#endif

/*int pcap_create(char *fname, pcap_file_t *pcap)
{
    pcap_file_header_t fheader_l ;

    if(fname == NULL || pcap == NULL)
    {
        return ERROR ;
    }

    if ((pcap->file = fopen(fname,"wb")) == NULL)
    {
        printf("error: couldn't create pcap file %s\n", fname) ;
        return ERROR ;
    }

    memcpy(pcap->fname, fname, 128) ;

    memset(&fheader_l, 0, sizeof(pcap_file_header_t));

    if(pcap->time_resolution == PCAP_MICRO_RESOLUTION)
    {
        fheader_l.magic    = PCAP_MAGIC ;
    }
    else if(pcap->time_resolution == PCAP_NANO_RESOLUTION)
    {
        fheader_l.magic    = PCAP_MAGIC_NS ;
    }
    else
    {
        printf("error: unknown pcap time resolution\n") ;
        pcap_close(pcap) ;
        return ERROR ;
    }

    pcap->max_length         = PCAP_DEFAULT_MAX_LENGTH ;

    fheader_l.version_major  = PCAP_VERSION_MAJOR ;
    fheader_l.version_minor  = PCAP_VERSION_MINOR ;

    if (pcap->link_type == PCAP_LINKTYPE_NULL)
    {
        pcap->link_type = PCAP_DEFAULT_LINKTYPE ;
    }

    fheader_l.max_length     = pcap->max_length ;
    fheader_l.link_type      = pcap->link_type ;

    if(pcap->byte_swap)
    {
        pcap_swap_fheader(&fheader_l) ;
    }

    if (fwrite((void *)&fheader_l, sizeof(pcap_file_header_t), 1, pcap->file) <= 0)
    {
        printf("error: couldn't write pcap header\n") ;
        pcap_close(pcap) ;
        return ERROR ;
    }

    fflush(pcap->file) ;
    return OK ;
}

int pcap_open(char *fname, pcap_file_t *pcap)
{
    pcap_file_header_t fheader_l ;

    if(pcap != NULL)
    {
        memset(pcap, 0, sizeof(pcap_file_t));
    }

    if(fname == NULL || pcap == NULL)
    {
        return ERROR ;
    }

    if ((pcap->file = fopen(fname,"rb")) == NULL)
    {
        printf("error: couldn't open pcap file %s\n", fname) ;
        return ERROR ;
    }

    memcpy(pcap->fname, fname, 128) ;

    memset(&fheader_l, 0, sizeof(pcap_file_header_t));

    if(fread((void *)&fheader_l, sizeof(pcap_file_header_t), 1, pcap->file) <= 0)
    {
        pcap_close(pcap) ;
        return ERROR ;
    }

    if(fheader_l.magic == PCAP_MAGIC || fheader_l.magic == PCAP_MAGIC_NS)
    {
        pcap->byte_swap = FALSE ;
    }
    else if(fheader_l.magic == PCAP_MAGIC_SWAP || fheader_l.magic == PCAP_MAGIC_NS_SWAP)
    {
        pcap->byte_swap = TRUE ;
        pcap_swap_fheader(&fheader_l) ;
    }
    else
    {
        printf("error: unknow file format (magic 0x%08x)\n", fheader_l.magic) ;
        pcap_close(pcap) ;
        return ERROR ;
    }

    //pcap_dump_fheader(&fheader_l) ;

    if(fheader_l.magic == PCAP_MAGIC_NS)
    {
        pcap->time_resolution = PCAP_NANO_RESOLUTION ;
    }
    else
    {
        pcap->time_resolution = PCAP_MICRO_RESOLUTION ;
    }

    pcap->link_type  = fheader_l.link_type ;
    pcap->max_length = fheader_l.max_length ;

    return OK ;
}*/

/*void pcap_close(pcap_file_t *pcap)
{
    fclose(pcap->file) ;
}

int pcap_read(pcap_file_t *pcap, char *buffer, HS_UINT32 buffer_len, pcap_packet_header_t *pktheader)
{
    pcap_packet_header_t pktheader_l ;

    if(pcap == NULL)
    {
        printf("error: pcap file ptr is null\n") ;
        return ERROR ;
    }

    memset(&pktheader_l, 0, sizeof(pcap_packet_header_t));

    if(fread((void *)&pktheader_l, sizeof(pcap_packet_header_t), 1, pcap->file) <= 0)
    {
        //printf("error: pcap can't read pkt header from %s (errno %d)\n", pcap->fname, errno) ;
        if(pktheader)
        {
            memset(pktheader, 0, sizeof(pcap_packet_header_t)) ;
        }
        return ERROR ;
    }

    if(pcap->byte_swap)
    {
        pcap_swap_pktheader(&pktheader_l) ;
    }

    if(pktheader)
    {
        memcpy(pktheader, &pktheader_l, sizeof(pcap_packet_header_t)) ;
    }

    if(buffer != NULL)
    {
        if(pktheader_l.caplen > buffer_len)
        {
            pktheader_l.caplen = buffer_len ;
            if(pktheader)
            {
                pktheader->caplen = buffer_len ;
            }
        }

        if(fread((void *)buffer, pktheader_l.caplen, 1, pcap->file) <= 0)
        {
            printf("error: pcap can't read pkt\n") ;
            if(pktheader)
            {
                memset(pktheader, 0, sizeof(pcap_packet_header_t)) ;
            }
            return ERROR ;
        }
    }

    return OK ;
}
*/
/*int pcap_write(pcap_file_t *pcap, char *buffer, HS_UINT32 caplen, HS_UINT32 wirelen, pcap_time_t *timestamp)
{
    pcap_packet_header_t pktheader_l ;
    OsTicks ticks ;

    ticks = osWallTicks() ;

    if(pcap == NULL)
    {
        printf("error: pcap file ptr is null\n") ;
        return ERROR ;
    }

    if(caplen == 0)
    {
        return OK ;
    }

    if(wirelen == 0 || caplen > wirelen)
    {
        wirelen = caplen ;
    }

    pktheader_l.caplen = caplen ;
    pktheader_l.len    = wirelen ;

    if(timestamp)
    {
        pktheader_l.timestamp.secs   = timestamp->secs ;
        pktheader_l.timestamp.frac   = timestamp->frac ;
    }
    else
    {
        struct timespec ots ;
        osTicksToTimespec(&ots, &ticks) ;
        pktheader_l.timestamp.secs   = ots.tv_sec ;
        pktheader_l.timestamp.frac   = ots.tv_nsec ;
    }

    if(pcap->byte_swap)
    {
        pcap_swap_pktheader(&pktheader_l) ;
    }

    if (fwrite((void *)&pktheader_l, sizeof(pcap_packet_header_t), 1, pcap->file) <= 0)
    {
        return ERROR ;
    }

    if ((buffer != NULL) && (fwrite((void *)buffer, caplen, 1, pcap->file) <= 0))
    {
        return ERROR ;
    }

    fflush(pcap->file) ;
    return OK ;
}

void pcap_swap_pktheader(pcap_packet_header_t *pktheader)
{
    pktheader->caplen  = SWAP32(pktheader->caplen) ;
    pktheader->len     = SWAP32(pktheader->len) ;

    pktheader->timestamp.secs = SWAP32(pktheader->timestamp.secs) ;
    pktheader->timestamp.frac = SWAP32(pktheader->timestamp.frac) ;
}

void pcap_swap_fheader(pcap_file_header_t *fheader)
{
    fheader->magic         = SWAP32(fheader->magic) ;

    fheader->version_major = SWAP16(fheader->version_major) ;
    fheader->version_minor = SWAP16(fheader->version_minor) ;

    fheader->time_zone     = SWAP32(fheader->time_zone) ;
    fheader->accuracy      = SWAP32(fheader->accuracy) ;

    fheader->max_length    = SWAP32(fheader->max_length) ;
    fheader->link_type     = SWAP32(fheader->link_type) ;
}

void pcap_dump_fheader(pcap_file_header_t *fheader)
{
    printf("    Magic: 0x%08x ....\n", fheader->magic) ;
    printf("  Version: %u.%u\n",       fheader->version_major,fheader->version_minor) ;
    printf("Time Zone: %d\n",          fheader->time_zone) ;
    printf("Time Accu: %u\n",          fheader->accuracy) ;
    printf("  Max Len: %u\n",          fheader->max_length) ;
    printf("Link Type: %u ....\n",     fheader->link_type) ;
}

void pcap_dump_pktheader(pcap_packet_header_t *pktheader)
{
    printf("pkt header: sec %u.%u - caplen %u, wirelen %u\n",
           pktheader->timestamp.secs,
           pktheader->timestamp.frac,
           pktheader->caplen,
           pktheader->len
           ) ;
}*/

